import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class a14 {

    public static int[][] merge(int[][] intervals) {
        Arrays.sort(intervals, Comparator.comparingInt(p -> p[0]));
        List<int[]> result = new ArrayList();
        for (int[] inter : intervals) {
            int m = result.size();
            if (m > 0 && inter[0] <= result.get(m - 1)[1]) {
                result.get(m - 1)[1] = Math.max(result.get(m - 1)[1], inter[1]);
            } else {
                result.add(inter);
            }
        }
        return result.toArray(new int[result.size()][]);
    }
    public static void main(String[] args) {
        int[][] nums = {{1, 3}, {2, 6}, {8, 10}, {15, 18}};
        System.out.println(merge(nums));
    }


}

